package com.ruicar.afs.cloud.demo.demo.contractgateway;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.mysql.cj.xdevapi.JsonArray;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.demo.demo.contractgateway.dto.PageQueryDto;
import com.ruicar.afs.cloud.demo.demo.contractgateway.dto.QueryCondition;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/demoContract")
@Slf4j
public class GateWayDemoController {
    @GetMapping("/getPageQuery")
    public IResponse getPageQueryDemo(@RequestParam("load") Long loadCount,@RequestParam("skip") Long skipCount,@RequestParam("level") String level,@RequestParam("changeType") String type){
        log.info("get==>count{},skip{},level{},type{}",loadCount,skipCount,level,type);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",10000L);
        jsonObject.put("datas",new JsonArray());
        for(int i=0;i<loadCount;i++){
            JSONObject temp = new JSONObject();
            temp.put("name","test-get"+i);
            temp.put("age",i+100);
            temp.put("level","测试测试"+i);
            temp.put("type","01");
            temp.put("contractNo","contract-no-"+i);
            jsonObject.getJSONArray("datas").add(temp);
        }
        return IResponse.success(jsonObject);
    }



    @PostMapping("/postPageQuery")
    public IResponse postPageQueryDemo(@RequestBody PageQueryDto pageQueryDto){
        log.info("post==>请求参数{}",JSONObject.toJSONString(pageQueryDto));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",10000L);
        jsonObject.put("datas",new JsonArray());
        for(int i=0;i<pageQueryDto.getLoadCount();i++){
            JSONObject temp = new JSONObject();
            temp.put("name","test-post"+i);
            temp.put("age",i+100);
            temp.put("level","测试测试"+i);
            temp.put("type","01");
            temp.put("contractNo","contract-no-"+i);
            jsonObject.getJSONArray("datas").add(temp);
        }
        return IResponse.success(jsonObject);
    }

    @GetMapping("/getContractInfo")
    public IResponse getContractInfo(@RequestParam("contractNo") String contractNo){
        log.info("get==>请求参数{}",contractNo);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","test-get");
        jsonObject.put("age",100);
        jsonObject.put("level","测试测试");
        jsonObject.put("type","01");
        jsonObject.put("contractNo",contractNo);
        return IResponse.success(jsonObject);
    }


    @PostMapping("/postContractInfo")
    public IResponse postContractInfo(@RequestBody QueryCondition queryCondition){
        log.info("post==>请求参数{}",queryCondition.getContractNo());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","test-post");
        jsonObject.put("age",100);
        jsonObject.put("level","测试测试");
        jsonObject.put("type","01");
        jsonObject.put("contractNo",queryCondition.getContractNo());
        jsonObject.put("repayInfo",JSONObject.parseObject("{\"repayPlay\":[{\"num\":0,\"money\":100,\"repayLog\":[{\"date\":\"2020-07-09\",\"money\":80},{\"date\":\"2020-07-10\",\"money\":20}]},{\"num\":1,\"money\":100,\"repayLog\":[{\"date\":\"2020-07-09\",\"money\":80},{\"date\":\"2020-07-10\",\"money\":20}]},{\"num\":2,\"money\":100,\"repayLog\":[{\"date\":\"2020-07-09\",\"money\":80},{\"date\":\"2020-07-10\",\"money\":20}]},{\"num\":3,\"money\":100,\"repayLog\":[{\"date\":\"2020-07-09\",\"money\":80},{\"date\":\"2020-07-10\",\"money\":20}]},{\"num\":4,\"money\":100,\"repayLog\":[{\"date\":\"2020-07-09\",\"money\":80},{\"date\":\"2020-07-10\",\"money\":20}]},{\"num\":5,\"money\":100,\"repayLog\":[{\"date\":\"2020-07-09\",\"money\":80},{\"date\":\"2020-07-10\",\"money\":20}]},{\"num\":6,\"money\":100},{\"num\":7,\"money\":100,\"repayLog\":[]}]}"));
        return IResponse.success(jsonObject);
    }
    @PostMapping("/testDateFormat")
    public IResponse postDate(@RequestBody TempDate tempDate){
        log.info(JSONObject.toJSONString(tempDate));
        return IResponse.success(tempDate);
    }

    @Data
    public static class TempDate{
        @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @JSONField(format="yyyy-MM-dd")
        Date date;
    }
}
